package com.web.app.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.web.app.dao.ClassInfoMapper;
import com.web.app.pojo.ClassInfo;
import com.web.app.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 班级管理表 前端控制器
 * </p>
 *
 * @author 林
 * @since 2022-09-21
 */
@RestController
@CrossOrigin
public class ClassInfoController {

    @Autowired
    private ClassInfoMapper classInfoMapper;


    @RequestMapping("/getShixun")
    public ResultUtil getShixun(int id) {
        ResultUtil resultUtil = ResultUtil.ok();

        List<ClassInfo> classInfos = classInfoMapper.getShixun(id);
        resultUtil.setData(classInfos);
        return resultUtil;
    }

    @RequestMapping("/getClassInfo")
    public ResultUtil getClassInfo(int id) {
        ResultUtil resultUtil = ResultUtil.ok();
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_teacher_id",id);
        List<ClassInfo> classInfos = classInfoMapper.selectList(queryWrapper);
        resultUtil.setData(classInfos);
        return resultUtil;
    }

    @RequestMapping("/addClassInfo")
    public ResultUtil addClassInfo(ClassInfo classInfo) {
        ResultUtil resultUtil = ResultUtil.ok();
        //根据当前信息查询数据库 如果有内容就不添加了
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",classInfo.getName());
        List<ClassInfo> classInfos = classInfoMapper.selectList(queryWrapper);
        if(classInfos != null && classInfos.size() > 0){
            return ResultUtil.error("班级名称已存在！添加失败");
        }
        //向数据库里插入这条数据
        classInfoMapper.insert(classInfo);
        resultUtil.setMsg("添加成功");
        return resultUtil;
    }

    @RequestMapping("/editClassInfo")
    public ResultUtil editClassInfo(ClassInfo classInfo) {
        ResultUtil resultUtil = ResultUtil.ok();
        System.out.println(classInfo);
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",classInfo.getName());
        List<ClassInfo> classInfos = classInfoMapper.selectList(queryWrapper);
        if(classInfos != null && classInfos.size() > 0){
            return ResultUtil.error("班级名称已存在！修改失败");
        }
        classInfoMapper.updateById(classInfo);
        resultUtil.setMsg("修改成功");
        return resultUtil;
    }

    @RequestMapping("/delClassInfo")
    public ResultUtil delClassInfo(int id) {
        ResultUtil resultUtil = ResultUtil.ok();
        try {
            classInfoMapper.deleteById(id);
            resultUtil.setMsg("删除成功");
        }catch (Exception e){
            return ResultUtil.error("删除失败");
        }
        return resultUtil;
    }
}

